package com.example.hms.controller;

import com.example.hms.servicelmpl.UserService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import com.example.hms.util.TokenUtil;
import org.springframework.web.bind.annotation.*;
import com.example.hms.entity.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/*两条注解缩减为一条*/@RestController
/*标识路径注解*/@RequestMapping("/user")
public class UserController {
    /*标识注解*/@Resource
    /*规定私有类*/private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map,   HttpServletRequest request) {
        String userId = map.get("userId");
        String userPassword = map.get("password");
        if (userId == null || userId.isEmpty()|| userPassword == null || userPassword.equals("")) {
            return ResultSubmit.error("登录失败,参数不全");
        }
        return userService.login(userId,userPassword,request);
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result info(HttpServletRequest request){
        return userService.info(request);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("token", null);
        return ResultSubmit.success("退出登录成功");
    }

    @RequestMapping(value = "/selectAllUser",method = RequestMethod.GET)/*查询*/
    public Result selectAllUser(String userId,String userName,String roleName){
        return userService.selectAllUser(userId,userName,roleName);
    }


    @RequestMapping(value = "/insert",method = RequestMethod.GET)/*新增*/
    public Result insert(User user){
        return userService.insert(user);
    }

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public Result update(User user){
        return userService.update(user);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)/*删除*/
    public Result delete(String userId){
        return userService.delete(userId);
    }
}


